<?php


namespace wechat\lib\core;


use wechat\lib\Http;

class Media extends Base
{
    static private $instance;

    private function __construct($appid,$appsecret)
    {
        $this->appid = $appid;
        $this->appsecret = $appsecret;
    }

    private function __clone()
    {
        // TODO: Implement __clone() method.
    }

    static public function getInstance($appid,$appsecret)
    {
        //判断$instance是否是Singleton的对象，不是则创建
        if (!self::$instance instanceof self) {
            self::$instance = new self($appid,$appsecret);
        }
        return self::$instance;
    }

    /**
     * @desc 上传临时图片素材
     * @param string $file
     * @return string
     */
    public function addImgTempMedia(string $file): string
    {
        if(!$this->extension($file,['png','jpeg','jpg','gif'])){
            $this->error('无效文件');
        }
        $str = $this->tempMedia('image',$file);
        if ($this->checkError($str)){
            return $str;
        }
        return '';
    }

    /**
     * @desc 上传临时语音素材
     * @param string $file
     * @return string
     */
    public function addVoiceTempMedia(string $file): string
    {
        if(!$this->extension($file,['amr','mp3'])){
            $this->error('无效文件');
        }
        $str = $this->tempMedia('voice',$file);
        if ($this->checkError($str)){
            return $str;
        }
        return '';
    }

    /**
     * @desc 上传临时视频素材
     * @param string $file
     * @return string
     */
    public function addVideoTempMedia(string $file): string
    {
        if(!$this->extension($file,['mp4'])){
            $this->error('无效文件');
        }
        $str = $this->tempMedia('video',$file);
        if ($this->checkError($str)){
            return $str;
        }
        return '';
    }

    /**
     * @desc 上传临时缩略图
     * @param string $file
     * @return string
     */
    public function addThumbTempMedia(string $file): string
    {
        if(!$this->extension($file,['jpg'])){
            $this->error('无效文件');
        }
        $str = $this->tempMedia('thumb',$file);
        if ($this->checkError($str)){
            return $str;
        }
        return '';
    }

    /**
     * @desc 上传图片永久素材
     * @param string $file
     * @return string
     */
    public function addImgMaterial(string $file): string
    {
        if(!$this->extension($file,['bmp','png','jpeg','jpg','gif'])){
            $this->error('无效文件');
        }
        $str = $this->material('image',$file);
        if ($this->checkError($str)){
            return $str;
        }
        return '';
    }

    /**
     * @desc 上传语音永久素材
     * @param string $file
     * @return string
     */
    public function addVoiceMaterial(string $file): string
    {
        if(!$this->extension($file,['mp3','wma','wav','amr'])){
            $this->error('无效文件');
        }
        $str = $this->material('voice',$file);
        if ($this->checkError($str)){
            return $str;
        }
        return '';
    }

    /**
     * @desc 上传视频永久素材
     * @param string $file
     * @param string $title
     * @param string $introduction
     * @return string
     */
    public function addVideoMaterial(string $file, string $title, string $introduction): string
    {
        if(!$this->extension($file,['mp4'])){
            $this->error('无效文件');
        }
        $str = $this->material('video',$file,$title,$introduction);
        if ($this->checkError($str)){
            return $str;
        }
        return '';
    }

    /**
     * @desc 上传缩略图永久素材
     * @param string $file
     * @return string
     */
    public function addThumbMaterial(string $file): string
    {
        if(!$this->extension($file,['jpg'])){
            $this->error('无效文件');
        }
        $str = $this->material('thumb',$file);
        if ($this->checkError($str)){
            return $str;
        }
        return '';
    }

    private function tempMedia(string $type,string $file):string
    {
        $url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=".$this->getGlobalAccessToken()."&type=$type";
        $data = array('media'=> new \CURLFile($file));
        $str = Http::curl('post',$url,$data);
        return $str;
    }

    public function material(string $type,string $file,string $title="",string $introduction=""):string
    {
        $url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=".$this->getGlobalAccessToken()."&type=$type";
        $data = array('media'=>new \CURLFile($file));
        if($type=='video'){
            $data = array('media'=>new \CURLFile($file),'description'=>json_encode(['title'=>$title,'introduction'=>$introduction]));
        }
        $str = Http::curl('post',$url,$data);
        return $str;
    }

    /**
     * @desc 上传图文素材
     * @param array $articles
     * @return string
     */
    public function addArticleMaterial(array $articles):string
    {
        $url = "https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=".$this->getGlobalAccessToken();
        $data = json_encode(["articles" => $articles]);
        $str = Http::curl('post',$url,$data);
        if ($this->checkError($str)){
            return $str;
        }
    }

    /**
     * @desc 上传图文素材图片
     * @param string $file
     * @return string
     */
    public function addArticleImgMaterial(string $file):string
    {
        if(!$this->extension($file,['jpg','png'])){
            $this->error('无效文件');
        }
        $url = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=".$this->getGlobalAccessToken();
        $data = new \CURLFile(['media'=>new \CURLFile($file)]);
        $str = Http::curl('post',$url,$data);
        if ($this->checkError($str)){
            return $str;
        }
    }

    /**
     * @desc 更新图文素材
     * @param string $media_id
     * @param array $article
     * @param int $index
     * @return bool
     */
    public function updateArticleMaterial(string $media_id,array $article,int $index=0):string
    {
        $url = "https://api.weixin.qq.com/cgi-bin/material/update_news?access_token=".$this->getGlobalAccessToken();
        $data = json_encode(["media_id"=>$media_id,"index"=>$index,"articles"=>$article]);
        $str = Http::curl('post',$url,$data);
        if ($this->checkError($str)){
            return $str;
        }
    }

    /**
     * @desc 获取临时素材
     * @param string $media_id
     * @return string
     */
    public function getTempMedia(string $media_id):string
    {
        $url = "https://api.weixin.qq.com/cgi-bin/media/get?access_token=".$this->getGlobalAccessToken()."&media_id=$media_id";
        $str = Http::curl('get',$url);
        if ($this->checkError($str)){
            return $str;
        }
    }

    /**
     * @desc 获取jssdk上传的素材
     * @param string $media_id
     * @return string
     */
    public function getJssdkMedia(string $media_id):string
    {
        $url = "https://api.weixin.qq.com/cgi-bin/media/get/jssdk?access_token=".$this->getGlobalAccessToken()."&media_id=$media_id";
        $str = Http::curl('get',$url);
        if ($this->checkError($str)){
            return $str;
        }
    }

    /**
     * @desc 获取永久素材
     * @param string $media_id
     * @return string
     */
    public function getMaterial(string $media_id):string
    {
        $url = "https://api.weixin.qq.com/cgi-bin/material/get_material?access_token=".$this->getGlobalAccessToken();
        $data = json_encode(['media_id'=>$media_id]);
        $str = Http::curl('post',$url,$data);
        if ($this->checkError($str)){
            return $str;
        }
    }

    /**
     * @desc 获取素材总数
     * @return string
     */
    public function getMaterialCount():string
    {
        $url = "https://api.weixin.qq.com/cgi-bin/material/get_materialcount?access_token=".$this->getGlobalAccessToken();
        $str = Http::curl('get',$url);
        if ($this->checkError($str)){
            return $str;
        }
    }

    /**
     * @desc 获取素材列表
     * @param string $type
     * @param int $offset
     * @param int $count
     * @return string
     */
    public function getMaterialList(string $type,int $offset,int $count):string
    {
        $url = "https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=".$this->getGlobalAccessToken();
        $data = json_encode(["type"=>$type,"offset"=>$offset,"count"=>$count]);
        $str = Http::curl('post',$url,$data);
        if ($this->checkError($str)){
            return $str;
        }
    }

    /**
     * @desc 删除素材
     * @param string $media_id
     * @return bool
     */
    public function delMaterial(string $media_id):bool
    {
        $url = "https://api.weixin.qq.com/cgi-bin/material/del_material?access_token=".$this->getGlobalAccessToken();
        $data = json_encode(['media_id'=>$media_id]);
        $str = Http::curl('post',$url,$data);
        return $this->checkError($str);
    }
}